package main

import (
	"fmt"
	"reflect"
)

func main() {
	stu := Student{
		Name: "zw",
		Age:  12,
	}
	reflect01(stu)

}

type Student struct {
	Name string
	Age  int
}

func reflect01(i interface{}) {
	// 获取传入变量的 type,值,kind
	rType := reflect.TypeOf(i)
	fmt.Println("rType=", rType) // rType= main.Student
	rValue := reflect.ValueOf(i)
	//  转为interface
	iV := rValue.Interface()
	fmt.Printf("iV=%v iV =%T \n", iV, iV) // iV={zw 12} iV =main.Student
	// 将 interface{} 通过断言转为需要的类型 --编译时无法确定，无法通过编译
	stu, ok := iV.(Student)
	if ok {
		fmt.Printf("stu.Name=%v\n", stu.Name)
	}
	// 获取 变量对应的kind --常量
	kind := rValue.Kind()
	kind2 := rValue.Kind()
	fmt.Print("kind=%v kind2 =%v \n", kind, kind2) // struct struct
}
